<template>
<el-card v-loading="isLoading">
  <el-form label-position="top">
    <el-form-item>
      <template #label>
        <strong>极强检测Token</strong>
      </template>
      <el-input v-model="token" size="small" style="max-width: 250px;" placeholder="请输入您的Token"></el-input>
      <el-button size="small" type="primary" style="margin-left: 5px" @click="save">保存</el-button>
    </el-form-item>
  </el-form>
  <div>
    <el-button size="small" type="primary" @click="dialogShow = true;">添加接口</el-button>
    <el-button size="small" type="danger" @click="confirmDelte(selectKeys)" :disabled="selectKeys.length == 0">删除选中</el-button>
  </div>
  <el-table :data="tableData" height="400px" v-loading="tableLoading" @selection-change="handleSelectionChange">
    <el-table-column
      type="selection"
      width="55"></el-table-column>
    <el-table-column label="ID" prop="id" width="80"></el-table-column>
    <el-table-column label="接口名称" prop="name"></el-table-column>
    <el-table-column label="接口地址" prop="api"></el-table-column>
    <el-table-column label="操作" width="150">
      <template #default="{ row }">
        <el-button type="danger" size="mini" @click="confirmDelte([row.id])">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</el-card>
<el-dialog title="添加接口" v-model="dialogShow" custom-class="add-interface-dialog">
  <el-form :model="form" label-position="top" size="small" ref="formRef" :rules="rules">
    <el-form-item label="接口名称" prop="name">
      <el-input v-model.trim="form.name" placeholder="请输入接口名称"></el-input>
    </el-form-item>
    <el-form-item label="接口地址" prop="url">
      <el-input v-model.trim="form.url" placeholder="请输入http|https开头的接口地址"></el-input>
    </el-form-item>
  </el-form>
  <template #footer>
    <el-button type="default" size="mini" @click="dialogShow = false">取消</el-button>
    <el-button type="primary" size="mini" @click="submit" :loading="isSubmit">添加</el-button>
  </template>
</el-dialog>
</template>

<script>
import { addApi, deleteApi, editApiToken, getApiList, getApiToken } from '@/axios/adminRequest'
import { getCurrentInstance, reactive, toRefs } from 'vue'

export default {
  name: 'setTokens',
  setup(){
    let { proxy } = getCurrentInstance()
    let state = reactive({
      token:'',
      isLoading:true,
      tableLoading:true,
      tableData:[],
      dialogShow:false,
      form:{
        name:'',
        url:'',
        api:'',
        data:'',
      },
      rules:{
        name:[{required:true,message:'请输入接口名称',trigger:'blur'}],
        url:[{required:true,message:'请输入接口地址',trigger:'blur'}],
      },
      isSubmit:false,
      selectKeys:[]
    })

    const getApi = () => {
      state.isLoading = true
      getApiToken().then(res=>{
        if(res.code == 0){
          state.token = res.data.token
        }else{
          proxy.$message.error(res.msg)
        }
        state.isLoading = false
      })
    }
    getApi()

    const save = () => {
      state.isLoading = true
      editApiToken({
        token:state.token
      }).then(res=>{
        if(res.code == 0){
          proxy.$message.success('保存成功')
        }else{
          proxy.$message.error(res.msg)
        }
        getApi()
      })

    }

    const getList = ()=>{
      state.tableLoading = true
      getApiList().then(res=>{
        if(res.code == 0){
          state.tableData = res.data
        }else{
          proxy.$message.error(res.data)
        }
        state.tableLoading = false
      })
    }

    getList()

    const submit = () => {
      proxy.$refs.formRef.validate(bool=>{
        if(bool){
          state.isSubmit = true
          let urlReg = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/
          let urlMatch = state.form.url.match(urlReg)
          state.form.api = urlMatch[1]
          let strArr = state.form.url.replace(urlMatch[1]+'?','').split('&')
          strArr.forEach((item,index)=>{
            index+1 !== strArr.length ? state.form.data += item.replace(/=.*/g,',') : state.form.data += item.replace(/=.*/g,'')
          })
          addApi(state.form).then(res=>{
            if(res.code == 0){
              proxy.$message.success('添加成功')
              proxy.$refs.formRef.resetFields()
              getList()
              state.dialogShow = false
            }else{
              proxy.$message.error(res.msg)
            }
            state.isSubmit = false
          })
        }
      })
    }

    const confirmDelte = (list) => {
      proxy.$confirm('确认要删除接口吗？','提示',{
        type:'error'
      }).then(()=>{
        state.tableLoading = true
        deleteApi({id:list}).then(res=>{
          if(res.code == 0){
            getList()
            proxy.$message.success('删除成功')
          }else{
            state.tableLoading = false
            proxy.$message.error(res.msg)
          }
        })
      })
    }

    const handleSelectionChange = (val)=>{
      let arr = []
      val.forEach(item=>{
        arr.push(item.id)
      })
      state.selectKeys = arr;
    }

    return{
      ...toRefs(state),
      save,submit,
      confirmDelte,
      handleSelectionChange
    }
  }
}
</script>

<style lang="less" scoped>
</style>
